﻿using MovingChame.WindowsStoreApp.Common;
using MovingChame.WindowsStoreApp.Helpers;
using MovingChameLibrary.Models;
using MovingChameLibrary.Models.Action;
using MovingChameLibrary.Models.MenuPage;
using MovingChameLibrary.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel.Search;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace MovingChame.WindowsStoreApp.UIModule
{
    public sealed partial class MainPage : LayoutAwarePage
    {
        private MenuViewModel _viewModel;
        public MenuViewModel ViewModel
        {
            get
            { return this._viewModel; }
            set
            {
                this._viewModel = value;
                this.DataContext = value;
            }
        }

        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            SearchPaneHelper.DeactivateSearchPane();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            SearchPaneHelper.Frame = this.Frame;
            SearchPaneHelper.ActivateSearchPane();
        }

        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            Geoposition currentPosition = await GeolocationHelper.GetCurrentLocation();
            ViewModel = new MenuViewModel(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
        }

        private void Header_Click(object sender, RoutedEventArgs e)
        {
            var group = (sender as FrameworkElement).DataContext;

            if (group.GetType().Name.Equals(typeof(OfficialTourGroup).Name))
                this.Frame.Navigate(typeof(OfficialTourPage));

            else if (group.GetType().Name.Equals(typeof(PersonalTourGroup).Name))
                this.Frame.Navigate(typeof(PersonalTourPage));
        }

        private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = e.ClickedItem;

            if (item.GetType().Name.Equals(typeof(TourGroup).Name))
                this.Frame.Navigate(typeof(TourSelectionPage), ((TourGroup)item).Id);

            else if (item.GetType().Name.Equals(typeof(CurrentTour).Name))
                this.Frame.Navigate(typeof(TourOverviewPage), (item as Tour).Id);

            else if (item.GetType().Name.Equals(typeof(SearchTourItem).Name))
                this.Frame.Navigate(typeof(AdvancedSearchPage));

            else if (item.GetType().Name.Equals(typeof(CurrentTourMapItem).Name))
                this.Frame.Navigate(typeof(CurrentTourMapPage), (item as Tour).Id);

            else if (item.GetType().Name.Equals(typeof(DefaultMenuItem).Name))
                DialogHelper.ShowInformDialog("No chameleons tour found!");

            else
                this.Frame.Navigate(typeof(TourOverviewPage), (item as Tour).Id);
        }
    }
}